﻿Imports System.IO
Imports System.IO.File
Imports System.Xml
Imports System.Net

Public Class FrmMachine

    Private Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
        If Button5.Text = "修改" Then
            DeviceType.ReadOnly = False
            Manufacturer.ReadOnly = False
            DeviceCode.ReadOnly = False
            DeviceId.ReadOnly = False
            Line.ReadOnly = False
            IP.ReadOnly = False
            StateFre.ReadOnly = False
            ProductFre.ReadOnly = False
            Family.ReadOnly = False
            TextBox_51SN.ReadOnly = False
            '
            MES_UploadAddress.ReadOnly = False
            XEnv.ReadOnly = False
            Token.ReadOnly = False
            UserName.ReadOnly = False
            SECTION.ReadOnly = False
            DirSwitchProd.ReadOnly = False
            '
            FirstCLearQuene.ReadOnly = False
            ClearQuene.ReadOnly = False
            ConfigMustLogin.ReadOnly = False
            '
            ScanIP.ReadOnly = False
            ScanProt.ReadOnly = False
            ScanStart.ReadOnly = False
            ScanEnd.ReadOnly = False
            ScanWhileTime.ReadOnly = False
            ScanWhileEnable.Enabled = False
            ScanEnable.Enabled = False
            '
            Button5.Text = "保存"
        ElseIf Button5.Text = "保存" Then
            'SaveData()
            machineInfo.Device_type = DeviceType.Text
            machineInfo.Manufacturer = Manufacturer.Text
            machineInfo.Device_Code = DeviceCode.Text
            machineInfo.Device_id = DeviceId.Text                    'MES 交互信息站别
            machineInfo.Line = Line.Text                             'MES 交互信息线别
            machineInfo.IP = IP.Text                                 'MES 交互访问地址
            StateFreTime = StateFre.Text
            ProductFreTime = ProductFre.Text
            SNInfo.Family = Family.Text
            machineInfo.IP_51 = TextBox_51SN.Text
            machineInfo.DirSwitchProd = DirSwitchProd.Text
            '
            machineInfo.MES_UploadAddress = MES_UploadAddress.Text
            machineInfo.XEnv = XEnv.Text
            machineInfo.Token = Token.Text
            machineInfo.UserName = UserName.Text
            machineInfo.SECTION = SECTION.Text
            machineInfo.FirstClearQuene = FirstCLearQuene.Text.Trim()
            machineInfo.ClearQuene = ClearQuene.Text.Trim()
            machineInfo.ConfigMustLogin = ConfigMustLogin.Text.Trim()
            '
            '
            PublicHikScanConfig.ScanIP = ScanIP.Text.Trim()
            PublicHikScanConfig.ScanProt = ScanProt.Text.Trim()
            PublicHikScanConfig.ScanStart = ScanStart.Text.Trim()
            PublicHikScanConfig.ScanEnd = ScanEnd.Text.Trim()
            PublicHikScanConfig.ScanWhileTime = ScanWhileTime.Text.Trim()
            If Val(PublicHikScanConfig.ScanWhileTime) < 200 Then
                PublicHikScanConfig.ScanWhileTime = 200
            End If
            PublicHikScanConfig.ScanWhileEnable = IIf(ScanWhileEnable.Checked, True, False)
            PublicHikScanConfig.ScanEnable = IIf(ScanEnable.Checked, True, False)
            '
            Dim Machine As New XmlTextWriter(Application.StartupPath & "\MachineInfo.xml", System.Text.Encoding.GetEncoding("utf-8"))
            Try
                Machine.WriteStartDocument()
                Machine.Formatting = Formatting.Indented
                Machine.WriteStartElement("MachineInfo")
                Machine.WriteElementString("DeviceType", machineInfo.Device_type)
                Machine.WriteElementString("Manufacturer", Manufacturer.Text)
                Machine.WriteElementString("DeviceCode", DeviceCode.Text)
                Machine.WriteElementString("DeviceId", DeviceId.Text)
                Machine.WriteElementString("Line", Line.Text)
                Machine.WriteElementString("IP", IP.Text)
                Machine.WriteElementString("StateFre", StateFreTime)
                Machine.WriteElementString("ProductFre", ProductFreTime)
                Machine.WriteElementString("Family", Family.Text)
                Machine.WriteElementString("IP_51", TextBox_51SN.Text)
                '
                Machine.WriteElementString("MES_UploadAddress", MES_UploadAddress.Text)
                Machine.WriteElementString("XEnv", XEnv.Text)
                Machine.WriteElementString("Token", Token.Text)
                Machine.WriteElementString("UserName", UserName.Text)
                Machine.WriteElementString("SECTION", SECTION.Text)
                Machine.WriteElementString("DirSwitchProd", DirSwitchProd.Text)
                '   '
                Machine.WriteElementString("ScanIP", PublicHikScanConfig.ScanIP)
                Machine.WriteElementString("ScanProt", PublicHikScanConfig.ScanProt)
                Machine.WriteElementString("ScanStart", PublicHikScanConfig.ScanStart)
                Machine.WriteElementString("ScanEnd", PublicHikScanConfig.ScanEnd)
                Machine.WriteElementString("ScanWhileTime", PublicHikScanConfig.ScanWhileTime)
                Machine.WriteElementString("ScanWhileEnable", PublicHikScanConfig.ScanWhileEnable)
                Machine.WriteElementString("ScanEnable", PublicHikScanConfig.ScanEnable)
                '
                Machine.WriteElementString("FirstCLearQuene", FirstCLearQuene.Text)
                Machine.WriteElementString("ClearQuene", ClearQuene.Text)
                Machine.WriteElementString("ConfigMustLogin", ConfigMustLogin.Text)
                '
                Machine.WriteEndElement()
                DeviceType.ReadOnly = True
                Manufacturer.ReadOnly = True
                DeviceCode.ReadOnly = True
                DeviceId.ReadOnly = True
                Line.ReadOnly = True
                IP.ReadOnly = True
                StateFre.ReadOnly = True
                ProductFre.ReadOnly = True
                Family.ReadOnly = True
                SECTION.ReadOnly = True
                TextBox_51SN.ReadOnly = True
                '
                MES_UploadAddress.ReadOnly = True
                XEnv.ReadOnly = True
                Token.ReadOnly = True
                UserName.ReadOnly = True
                DirSwitchProd.ReadOnly = True
                FirstCLearQuene.ReadOnly = True
                ClearQuene.ReadOnly = True
                ConfigMustLogin.ReadOnly = True
                '
                '
                ScanIP.ReadOnly = True
                ScanProt.ReadOnly = True
                ScanStart.ReadOnly = True
                ScanEnd.ReadOnly = True
                ScanWhileTime.ReadOnly = True
                ScanWhileEnable.Enabled = True
                ScanEnable.Enabled = True
                '
                Button5.Text = "修改"
                'FrmMain.UploadStatus.Interval = StateFreTime * 1000
                'FrmMain.UploadMainInfo.Interval = ProductFreTime * 1000
                MsgBox("设备信息保存成功！")
            Catch ex As Exception
                MsgBox("设备信息保存失败!" & ex.Message.ToString)
            Finally
                Machine.Close()
                'FrmMain.InitMachineInfo()
            End Try
        End If

    End Sub

    Private Sub FrmMachine_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Dim xmlpath = Application.StartupPath & "\MachineInfo.xml"
        If Exists(xmlpath) Then
            DeviceType.Text = machineInfo.Device_type
            Manufacturer.Text = machineInfo.Manufacturer
            DeviceCode.Text = machineInfo.Device_Code
            DeviceId.Text = machineInfo.Device_id
            Line.Text = machineInfo.Line
            IP.Text = machineInfo.IP
            StateFre.Text = StateFreTime
            ProductFre.Text = ProductFreTime
            Family.Text = SNInfo.Family
            TextBox_51SN.Text = machineInfo.IP_51
            '
            MES_UploadAddress.Text = machineInfo.MES_UploadAddress
            XEnv.Text = machineInfo.XEnv
            Token.Text = machineInfo.Token
            UserName.Text = machineInfo.UserName
            SECTION.Text = machineInfo.SECTION
            DirSwitchProd.Text = machineInfo.DirSwitchProd
            FirstCLearQuene.Text = machineInfo.FirstClearQuene
            ClearQuene.Text = machineInfo.ClearQuene
            ConfigMustLogin.Text = machineInfo.ConfigMustLogin
            '
            '
            ScanIP.Text = PublicHikScanConfig.ScanIP
            ScanProt.Text = PublicHikScanConfig.ScanProt
            ScanStart.Text = PublicHikScanConfig.ScanStart
            ScanEnd.Text = PublicHikScanConfig.ScanEnd
            ScanWhileTime.Text = PublicHikScanConfig.ScanWhileTime
            ScanWhileEnable.Checked = PublicHikScanConfig.ScanWhileEnable
            ScanEnable.Checked = PublicHikScanConfig.ScanEnable
            '
        End If
    End Sub

    Private Sub IP_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles IP.KeyPress
        'If Char.IsDigit(e.KeyChar) OrElse e.KeyChar = Chr(8) OrElse e.KeyChar = "." Then
        '    e.Handled = False
        'Else
        '    e.Handled = True
        '    MsgBox("只能输入数值或'.'")
        'End If
    End Sub

    'Private Sub SaveData()
    '    Dim xmlpath = Application.StartupPath & "\数据上传程序\Param.xml"
    '    Dim doc As XmlDocument = New XmlDocument
    '    doc.Load(xmlpath)    '加载Xml文件
    '    Dim rootElem As XmlElement = doc.DocumentElement
    '    Dim nodeList As XmlNodeList = rootElem.ChildNodes
    '    For Each xn In nodeList '遍历所有子节点
    '        Dim xe As XmlElement = CType(xn, XmlElement) '将子节点类型转换为XmlElement类型
    '        If xe.Name = "MachineStatusUploadPath" Then
    '            xe.InnerText = "Share/" & Line.Text & "/" & DeviceCode.Text & "/MachineStatus/"
    '        ElseIf xe.Name = "MainInfoUploadPath" Then
    '            xe.InnerText = "Share/" & Line.Text & "/" & DeviceCode.Text & "/MainInfo/"
    '        ElseIf xe.Name = "SNInfoUploadPath" Then
    '            xe.InnerText = "Share/" & Line.Text & "/" & DeviceCode.Text & "/SNInfo/"
    '        ElseIf xe.Name = "SNDetailUploadPath" Then
    '            xe.InnerText = "Share/" & Line.Text & "/" & DeviceCode.Text & "/SNDetail/"
    '        End If
    '    Next
    '    doc.Save(xmlpath)
    '    'Dim MachineData As New XmlTextWriter(Application.StartupPath & "\数据上传程序\Param.xml", System.Text.Encoding.GetEncoding("utf-8"))
    '    'MachineData.WriteStartDocument()
    '    'MachineData.Formatting = Formatting.Indented
    '    'MachineData.WriteStartElement("Param")
    '    'MachineData.setattribute("MachineStatusUploadPath", "Share/FA_" & Line.Text & "/" & DeviceCode.Text & "/MachineStatus/")
    '    'MachineData.WriteElementString("MainInfoUploadPath", "Share/FA_" & Line.Text & "/" & DeviceCode.Text & "/MainInfo/")
    '    'MachineData.WriteElementString("SNInfoUploadPath", "Share/FA_" & Line.Text & "/" & DeviceCode.Text & "/SNInfo/")
    '    'MachineData.WriteElementString("SNDetailUploadPath", "Share/FA_" & Line.Text & "/" & DeviceCode.Text & "/SNDetail/")
    '    'MachineData.WriteEndElement()
    '    'MachineData.Close()
    'End Sub
End Class